<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of homepage
 *
 * @author sonUIT
 */
class Homepage extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library("loadpage");
    }

    public function timkiem($tentp) {
        $data = array();
        $lp = new loadpage();
        $this->load->model("mproduct");
        $sach = new Mproduct();
        $data['products'] = array();
        $data['products'] = $sach->timkiem($tentp, NULL);
        $data['title'] = "Sách theo thể loại";
        $data['tacgias'] = array(array());
        $data['nxbs'] = array(array());
        $this->load->model("mtacgia");
        $this->load->model("mnhaxuatban");
        $nxb = new Mnhaxuatban();
        $tacgia = NEW Mtacgia();
        foreach ($data['products'] as $row) {
            $tacgia->GetByMatg($row["MATG"]);
            array_push($data['tacgias'], $tacgia->ToArray());
            $nxb->GetByManxb($row['MANXB']);
            array_push($data['nxbs'], $nxb->ToArray());
        }

        $this->load->model("mtheloai");

        $theloai = new Mtheloai();
        $data['theloai'] = $theloai->getListObject();
        $data['tukhoa'] = $tentp;
        $lp->load(array('ketquatksach_view'), $data);
    }

    public function index() {
        $data = array();
        $lp = new loadpage();
        if (isset($_GET['matp'])) {
            $data['title'] = "Chi tiết sản phẩm";
            $lp->load(array("product_detail_view"), $data);
        } else
        if (isset($_GET['matl'])) {
            $matl = $_GET['matl'];
            $this->load->model("mproduct");
            $sach = new Mproduct();
            $data['products'] = array();
            $data['products'] = $sach->GetSachByTheLoai($matl);
//  var_dump($data['products']);
            $data['title'] = '';
            $data['tacgias'] = array(array());
            $data['nxbs'] = array(array());
            $this->load->model("mtacgia");
            $this->load->model("mnhaxuatban");
            $nxb = new Mnhaxuatban();
            $tacgia = NEW Mtacgia();
            foreach ($data['products'] as $row) {
                $tacgia->GetByMatg($row["MATG"]);
                array_push($data['tacgias'], $tacgia->ToArray());
                $nxb->GetByManxb($row['MANXB']);
                array_push($data['nxbs'], $nxb->ToArray());
            }
            $this->load->model("mtheloai");

            $theloai = new Mtheloai();
            $data['theloai'] = $theloai->getListObject();
            $lp->load(array('slideshow_view', 'sachtheotheloai_view'), $data);
        } else {
            if (isset($_GET['sachmoi'])) {
                $sachmoi = $_GET['sachmoi'];
                $this->load->model("mproduct");
                $sach = new Mproduct();
                $data['products'] = array();
                $data['products'] = $sach->GetSachBySachmoi($sachmoi);
//  var_dump($data['products']);
                $data['title'] = "Sách Mới ";
                $data['tacgias'] = array(array());
                $data['nxbs'] = array(array());
                $this->load->model("mtacgia");
                $this->load->model("mnhaxuatban");
                $nxb = new Mnhaxuatban();
                $tacgia = NEW Mtacgia();
                foreach ($data['products'] as $row) {
                    $tacgia->GetByMatg($row["MATG"]);
                    array_push($data['tacgias'], $tacgia->ToArray());
                    $nxb->GetByManxb($row['MANXB']);
                    array_push($data['nxbs'], $nxb->ToArray());
                }
                $this->load->model("mtheloai");

                $theloai = new Mtheloai();
                $data['theloai'] = $theloai->getListObject();
                $lp->load(array('slideshow_view', 'sachtheotheloai_view'), $data);
            } else {
                if (isset($_GET['sachnoibat'])) {
                    $sachnoibat = $_GET['sachnoibat'];
                    $this->load->model("mproduct");
                    $sach = new Mproduct();
                    $data['products'] = array();
                    $data['products'] = $sach->GetSachBySachNoiBat($sachnoibat);
//  var_dump($data['products']);
                    $data['title'] = "Sách nổi bật ";
                    $data['tacgias'] = array(array());
                    $data['nxbs'] = array(array());
                    $this->load->model("mtacgia");
                    $this->load->model("mnhaxuatban");
                    $nxb = new Mnhaxuatban();
                    $tacgia = NEW Mtacgia();
                    foreach ($data['products'] as $row) {
                        $tacgia->GetByMatg($row["MATG"]);
                        array_push($data['tacgias'], $tacgia->ToArray());
                        $nxb->GetByManxb($row['MANXB']);
                        array_push($data['nxbs'], $nxb->ToArray());
                    }
                    $this->load->model("mtheloai");

                    $theloai = new Mtheloai();
                    $data['theloai'] = $theloai->getListObject();
                    $lp->load(array('slideshow_view', 'sachtheotheloai_view'), $data);
                } else {
                    $data['title'] = "AIO Book Store online";
                    $this->load->model('mproduct');
                    $mproducts = new Mproduct();
                    $data['products'] = $mproducts->GetAllProducts();
                    $data['tacgias'] = array(array());
                    $data['nxbs'] = array(array());
                    $this->load->model("mtacgia");
                    $this->load->model("mnhaxuatban");
                    $nxb = new Mnhaxuatban();
                    $tacgia = NEW Mtacgia();
                    foreach ($data['products'] as $row) {
                        $tacgia->GetByMatg($row["MATG"]);
                        array_push($data['tacgias'], $tacgia->ToArray());
                        $nxb->GetByManxb($row['MANXB']);
                        array_push($data['nxbs'], $nxb->ToArray());
                    }
//$data['theloai']=1;
                    $this->load->model("mtheloai");
                    $theloai = new Mtheloai();
                    $data['theloai'] = $theloai->getListObject();
//  var_dump( $data['theloai']);
                    $lp->load(array("slideshow_view", "products_view"), $data);
                }
            }
        }
    }

    function doimatkhau() {
        $lp = new loadpage();
        $data = array();
        $data['tentk'] = $_GET['tentk'];
        $data['title'] = "Đổi mật khẩu";
        $lp->load(array("doipass_view"), $data);
    }
}
?>
